<?php
namespace App\Controller;
use Think\Controller;
use App\Controller\BaseController;
class UserController extends BaseController {

    /**
     * 测试数据
     * @author xuan
     * @param id
     */
	public function test(){
        $file = 'Public/2.txt';
        var_dump(file_exists($file));die;
        echo substr($file,1);die;
        echo $file.'<pre>';
        $result = unlink($file);
        var_dump($result);
    }

    /**
    * 发送短信验证码
    * @author chen
    * @param user_phone      要发送的手机号码
    */
    public function sendMsgCodeApp(){
        $json_arr = $this->Security(false);
//         $json_arr['user_phone'] = '17301181236';
        if (empty($json_arr['user_phone'])) {
            $this->backToClient(2,'手机号码不能为空');
        }

        $phonenums = $json_arr['user_phone'];
        $code = createRandomCode(4);
        alisms($code, $phonenums); //发送短信
        $this->backToClient(1,'获取短信验证码成功',$code);
    }

    /**
     * 用户注册
     * @author chen
     * @param user_phone    用户电话号码
     * @param user_password 密码
     */
    public function register(){
        $json_arr = $this->Security(false);
        // $json_arr['user_phone'] = 18812345678;
        // $json_arr['user_password'] = md5('123456'.C(AES_KEY));
        if (empty($json_arr['user_phone']) || empty($json_arr['user_password'])) {
            $this->backToClient(2,'参数不全');
        }
        //当是通过扫用户分享码时需进行判断
        $user_model = M('user');
        $condition['user_phone'] = $json_arr['user_phone'];
        if($user_model->where($condition)->find()){
            $this->backToClient(4,'号码已被注册');
        }
        $json_arr['user_name'] = phonenumHidden($json_arr['user_phone']);
        $json_arr['user_regtime'] = $json_arr['user_lasttime'] = time();
        $json_arr['user_password'] = md5($json_arr['user_password'].C(AES_KEY));
        if($user_id = $user_model->add($json_arr)){
            $user_sig = $user_model->save([
                'user_id'=>$user_id,
                'user_sig'=>$this->getUserSig($user_id),
                'user_sigexpire'=>time() + 60*60*24*150, //150天过期时间
                'user_conference_room'=>$user_id - 990000, //生成会议室房间号
            ]);
            if($user_sig){
                $user_arr = $user_model
                    ->field('user_id_introducer,user_regtime,user_lasttime,user_display',true)
                    ->find($user_id);
                if($user_arr){
                    $this->backToClient(1,'注册成功',$user_arr);
                }else{
                    $this->backToClient(7,'注册成功，但返回数据失败');
                }
            }else{
                $user_model->where(['user_id'=>$user_id])->delete();
                $this->backToClient(6,'注册失败');
            }
        }else{
            $this->backToClient(5,'注册失败');
        }
    }

    /**
     * 用户登录
     * @author chen
     * @param user_phone            手机号
     * @param user_password         密码
     */
    public function login(){
        $json_arr = $this->Security(false);
        // $json_arr['user_phone'] = 17301181236;
        // $json_arr['user_password'] = md5('123456'.C(AES_KEY));
        if (empty($json_arr['user_phone']) || empty($json_arr['user_password'])) {
            $this->backToClient(2,'参数不全');
        }
        $json_arr['user_password'] = md5($json_arr['user_password'].C(AES_KEY));
        if($user_arr = M('user')->where($json_arr)->find()){
            if ($user_arr['user_display'] == 0) {
                $this->backToClient(4,'该账号存在非法操作，详情请咨询客服');
            }
            $condition['user_lasttime'] = time();
            $condition['user_id'] = $user_arr['user_id'];
            $condition['user_token'] = createRandom();
//            //判断user_sig是否过期
           if($user_arr['user_sigexpire'] < time()){
               $condition['user_sig'] = $this->getUserSig($user_arr['user_id']);
               $condition['user_sigexpire'] = time() + 60*60*24*150; //150天过期时间
           }
            M('user')->save($condition);
            $return_data = M('user')
                ->field('user_id_introducer,user_regtime,user_lasttime,user_display',true)
                ->find($user_arr['user_id']);
            $this->backToClient(1,'登录成功',$return_data);
        }else{
            $this->backToClient(3,'账号或密码错误');
        }
    }

    /**
     * 获取个人信息
     * @author chen
     * @param user_id       user_id（必传）
     */
    public function getUserinfo(){
        $json_arr = $this->Security();
//         $json_arr['user_id'] = 1000021;
        $user = M('user')
            ->field('user_id_introducer,user_regtime,user_lasttime,user_display',true)
            ->find($json_arr['user_id']);
        if ($user) {
            $this->backToClient(1,'获取用户信息成功',$user);
        }else{
            $this->backToClient(2,'用户不存在');
        }
    }

    /**
     * 忘记密码
     * @author chen
     * @param user_phone    手机号码（必填）
     * @param user_password 密码（必填）
     */
    public function resetPassword(){
        $json_arr = $this->Security(false);
//         $json_arr['user_phone'] = 17301181236;
//         $json_arr['user_password'] = md5('123456'.C(AES_KEY));
        if (empty($json_arr['user_phone']) || empty($json_arr['user_password'])) {
            $this->backToClient(2,'参数不全');
        }
        $condition['user_phone'] = $json_arr['user_phone'];
        $user_arr = M('user')->field('user_id')->where($condition)->find();
        if (empty($user_arr)) {
            $this->backToClient(3,'该手机号码未被注册');
        }
        $user_arr['user_password'] = md5($json_arr['user_password'].C(AES_KEY));
        $save_res = M('user')->save($user_arr);
        if($save_res){
            $this->backToClient(1,'重置密码成功！');
        }else{
            $this->backToClient(4,'重置密码失败！');
        }

    }

    /**
     * 修改密码
     * @author chen
     * @param user_id               用户id（必填）
     * @param user_password         密码（必填）
     * @param user_newpassword      新密码（必填）
     */
    public function updatePassword(){
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1000029;
        // $json_arr['user_password'] = md5('111111'.C(AES_KEY));
        // $json_arr['user_newpassword'] = md5('123456'.C(AES_KEY));
        if (empty($json_arr['user_id']) || empty($json_arr['user_password']) || empty($json_arr['user_newpassword'])) {
            $this->backToClient(2,'参数不全');
        }
        $user_arr = M('user')->field('user_id,user_password')->find($json_arr['user_id']);
        if (empty($user_arr)) {
            $this->backToClient(5,'用户不存在');
        }else{
            if (md5($json_arr['user_password'].C(AES_KEY)) == $user_arr['user_password']) {
                $save_arr['user_password'] = md5($json_arr['user_newpassword'].C(AES_KEY));
								$save_arr['user_id'] = $json_arr['user_id'];
                if (M('user')->save($save_arr)) {
                    $this->backToClient(1,'修改密码成功');
                }else{
                    $this->backToClient(4,'新密码和旧密码重复');
                }
            }else{
                $this->backToClient(3,'原始密码错误');
            }
        }

    }

    /**
     * 修改个人信息
     * @author chen
     * @param user_id           id（必传）
     * @param user_avatar        用户头像（选填）
     * @param user_name        真实姓名（选填）
     * @param user_sex          性别 1男 2女（选填）
     * @param user_birthday          生日（选填）
     * @param user_city          城市（选填）
     * @param user_autograph          签名（选填）
     * 注：至少有一项不为空
     */
    public function editUserinfo(){
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
//         $json_arr['user_avatar'] = 'http://xuancode.com/Upload/image/201710/17/ea25d2.jpg';
//         $json_arr['user_name'] = '王蕾';
//         $json_arr['user_sex'] = 2;
//         $json_arr['user_birthday'] = '1990-12-01';
//         $json_arr['user_city'] = '北京市昌平区';
//         $json_arr['user_autograph'] = '有心，有行';
//        $json_arr['user_notice'] = 1;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
        if(isset($json_arr['user_sex']) && !in_array($json_arr['user_sex'],array(1,2))){
            $this->backToClient(3,'性别参数不正确');
        }
        $user_model = M('user');
        $find_res = $user_model->find($json_arr['user_id']);
        if($find_res){
            $res = $user_model->save($json_arr);
            if($res){
                $this->backToClient(1,'个人资料更新成功');
            }else{
                $this->backToClient(4,'修改失败或提交资料与原资料相同');
            }
        }else{
            $this->backToClient(5,'用户不存在');
        }
    }

    /*
     * 单图片上传
     * author chen
     * @param file_name  上传的文件名称(必填)
     */
    public function uploadFile(){
//        $json_arr = $this->Security();
        if(empty($_FILES['file_name']['name'])){
            $this->backToClient(2,'参数不全');
        }
        $finfo = upload($_FILES['file_name']);
        if ($finfo['status'] == 0) {
            $this->backToClient(3,$finfo['msg']); //上传失败
        }else{
            $this->backToClient(1,'上传成功',$finfo['filepath']);
        }
    }
    ###图片上传测试页面
    public function uploadUserAvatarTest()
    {
        $this->display();
    }

    /*
     * 获取用户分享码(二维码)
     * author chen
     * @param user_id      用户ID(必填)
     */
    public function getUserCode()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
        //查询该用户是否生成二维
        $user_model = M('user');
        $user_info = $user_model
            ->field('user_id,user_code')
            ->find($json_arr['user_id']);
        if(empty($user_info)){
            $this->backToClient(2,'不存在该用户');
        }
        if(empty($user_info['user_code'])){
            //生成二维码
            //判断二维码图片是否存在
            $filename = 'Upload/image/user_qrcode/'.$user_info['user_id'].'.png';
            $url = C('DOMAIN_NAME').'/index.php/App/User/H5register/user_id/'.$user_info['user_id'];
            qrcode($url,$filename);
            //检测二维码文件是否真实生成
            if(file_exists($filename)){
                $updata = array('user_id'=>$user_info['user_id'],'user_code'=>'/'.$filename);
                $up_res = $user_model->save($updata);
                if($up_res){
                    $this->backToClient(1,'获取成功',$updata['user_code']);
                }else{
                    $this->backToClient(4,'获取失败');
                }
            }else{
                $this->backToClient(3,'生成二维码失败');
            }
        }else{
            $this->backToClient(1,'获取成功',$user_info['user_code']);
        }
    }

    /*
     * 添加收货地址 只有一个地址时为默认收货地址
     * author chen
     * @param user_id      用户ID(必填)
     * @param user_address_user_name    收货人(必填)
     * @param user_address_user_phone    收货人电话(必填)
     * @param user_address    收货详细地址(必填)
     * @param user_address_default    是否默认收货地址(选填)
     */
    public function addUserAddress()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
//        $json_arr['user_address_user_name'] = 'test2';
//        $json_arr['user_address_user_phone'] = '18866639123';
//        $json_arr['user_address'] = '北京昌平22';
//        $json_arr['user_address_default'] = 1;
        if(empty($json_arr['user_id']) || empty($json_arr['user_address_user_name']) || empty($json_arr['user_address_user_phone']) || empty($json_arr['user_address'])){
            $this->backToClient(2,'参数不全');
        }
        //查询用户是否有添加过收货地址
        $user_address_model = M('user_address');
        $user_address_res = $user_address_model
            ->where(array('user_id'=>$json_arr['user_id']))
            ->getField('user_address_default',true);
        if(count($user_address_res) <= 0){
            $json_arr['user_address_default'] = 1; //第一条地址为默认地址
        }else if($json_arr['user_address_default'] == 1){
            //将用户其它所有地址改成非默认
						$user_address_model
						->where(['user_id'=>$json_arr['user_id']])
						->save(['user_address_default'=>0]);
        }
        $json_arr['user_address_createtime'] = time();
        $add_res = $user_address_model->add($json_arr);
        if($add_res){
            $this->backToClient(1,'添加成功');
        }else{
            $this->backToClient(4,'添加失败');
        }
    }

    /*
     * 设置默认收货地址
     * author chen
     * @param user_id      用户ID(必填)
     * @param user_address_id    收货地址ID(必填)
     */
    public function setUserAddressDefault()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
//        $json_arr['user_address_id'] = 7;
        $json_arr['user_address_default'] = 1;
        if(empty($json_arr['user_id']) || empty($json_arr['user_address_id'])){
            $this->backToClient(2,'参数不全');
        }
        $user_address_model = M('user_address');
        $edit_default_address = $user_address_model
            ->where(array('user_id'=>$json_arr['user_id']))
            ->save(array('user_address_default'=>0));
        if($edit_default_address){
            $set_default_address = $user_address_model->save($json_arr);
            if($set_default_address){
                $this->backToClient(1,'设置成功');
            }else{
                $this->backToClient(4,'设置失败');
            }
        }else{
            $this->backToClient(3,'设置失败');
        }
    }

    /*
     * 获取收货地址
     * author chen
     * @param user_id   用户ID(必选)
     */
    public function getUserAddressDefault()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
        if(empty($json_arr['user_id'])){
            $this->backToClient(2,'参数不全');
        }
        $info = M('user_address')
            ->field('user_address_id,user_address_user_name,user_address_user_phone,user_address')
            ->where(['user_id'=>$json_arr['user_id'],'user_address_default'=>1])
            ->find();
        if($info){
            $this->backToClient(1,'获取成功',$info);
        }else{
            $this->backToClient(3,'该默认地址不存在');
        }
    }

    /*
     * 修改收货地址
     * author chen
     * @param $user_address_id   默认收货地址ID(必选)
     * @param user_address_user_name    收货人(必填)
     * @param user_address_user_phone    收货人电话(必填)
     * @param user_address    收货详细地址(必填)
     * @param user_address_default    是否默认收货地址(选填)
     */
    public function updateUserAddress()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
//        $json_arr['user_address_id'] = 5;
//        $json_arr['user_address_user_name'] = 'update23';
//        $json_arr['user_address_user_phone'] = '18866639123';
//        $json_arr['user_address'] = '北京昌平234';
//        $json_arr['user_address_default'] = 1;
        if(empty($json_arr['user_id']) || empty($json_arr['user_address_id']) || empty($json_arr['user_address_user_name']) || empty($json_arr['user_address_user_phone']) || empty($json_arr['user_address'])){
            $this->backToClient(2,'参数不全');
        }
        //查询用户是否有默认收货地址
        $user_address_model = M('user_address');
        if($json_arr['user_address_default'] == 1){
            $update_user_address_res = $user_address_model
                ->where(array('user_id'=>$json_arr['user_id']))
                ->save(array('user_address_default'=>0));
        }
        $save_res = $user_address_model->save($json_arr);
        if($save_res){
            $this->backToClient(1,'修改成功');
        }else{
            $this->backToClient(3,'修改失败');
        }
    }

    /*
     * 获取用户收货地址列表
     * author chen
     * @param $user_id   用户ID(必选)
     */
    public function getUserAddressList()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
        if (empty($json_arr['user_id'])) {
            $this->backToClient(2,'参数不全');
        }
//        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
//        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $address_list = M('user_address')
            ->where(array('user_id'=>$json_arr['user_id']))
            ->select();
        $this->backToClient(1,'获取成功',$address_list);
    }

    /*
     * 删除收货地址
     * @author chen
     * @param  user_address_id   用户收货地址ID
     */
    public function delUserAddress()
    {
        $json_arr = $this->Security();
//        $json_arr['user_address_id'] = 5;
        if(empty($json_arr['user_address_id'])){
            $this->backToClient(2,'参数不全');
        }
        //查询用户是否有默认收货地址
        $del_res = M('user_address')->delete($json_arr['user_address_id']);
        if($del_res){
            $this->backToClient(1,'删除成功');
        }else{
            $this->backToClient(3,'删除失败');
        }
    }

    /*
     * 用户意见反馈
     * @author chen
     * @param suggest_user_id  用户ID(必填)
     * @param suggest_type   反馈类型 1产品建议 2程序错误 3缺少视频 (必填)
     * @param suggest_content 反馈内容 （必填)
     * @param suggest_img 反馈图片 (可选)
     */
    public function userToSuggest()
    {
        $json_arr = $this->Security();
//        $json_arr['suggest_user_id'] = 1000021;
//        $json_arr['suggest_type'] = 1;
//        $json_arr['suggest_content'] = '希望产品种类再多点11';
//        $json_arr['suggest_img'] = 'http://www.baidu.com';
        $json_arr['suggest_createtime'] = time();
        if(empty($json_arr['suggest_user_id']) || empty($json_arr['suggest_type']) || empty($json_arr['suggest_content'])){
            $this->backToClient(2,'参数不全');
        }
        $add_res = M('suggest')->add($json_arr);
        if($add_res){
            $this->backToClient(1,'反馈成功');
        }else{
            $this->backToClient(3,'反馈失败');
        }
    }

    /*
     * 商城页面数据
     * @author chen
     */
    public function getGoodsIndexData()
    {
        $json_arr = $this->Security();
        //轮播数据
        $banner_data = $this->getBannerData(2);
//        var_dump($banner_data);
        //热门商品数据
        $hot_goods_ids = $this->getHotRecommendData(1);
        $hot_goods_where['goods_id'] = array('in',$hot_goods_ids);
        $hot_goods_data = $this->getGoodsPageData(1,3,$hot_goods_where);
        $hot_new_data = [];
        //热门商品按照热门表中顺序返回商品信息故作些处理
        foreach ($hot_goods_ids as $v) {
            foreach ($hot_goods_data as $val) {
                if($v == $val['goods_id']){
                    $hot_new_data[] = $val;
                }
            }
        }
        $hot_goods_data = $hot_new_data;
        //好物上新前6个商品
        $goods_data = $this->getGoodsPageData(1,6);
//        var_dump($goods_data);
        $this->backToClient(1,'获取成功',
            array(
                'banner_data'=>$banner_data,
                'hot_goods_data'=>$hot_goods_data,
                'goods_data'=>$goods_data
            )
        );
    }

    //获取热门数据 $type 1商品 2点播
    private function getHotRecommendData($type)
    {
        return M('hot_recommend')
            ->where(array('hot_recommend_type'=>$type))
            ->order('hot_recommend_createtime')
            ->getField('hot_recommend_ids',true);
    }

    //获取banner数据   //param $type 1名师汇 2商城
    private function getBannerData($type)
    {
        return M('banner')
            ->field('banner_id,banner_name,banner_pic,banner_url')
            ->where(array('banner_type'=>$type,'banner_display'=>1))
            ->order('banner_sort desc,banner_createtime')
            ->select();
    }
    //获取商品数据
    private function getGoodsPageData($p=1,$pnum=9,$where=array())
    {
        $where['goods_display'] = array('eq',1);
        return M('goods')
            ->where($where)
            ->field('goods_id,goods_name,goods_pic1,goods_price,goods_price_vip,goods_sale_num')
            ->order('goods_createtime desc')
            ->page($p,$pnum)
            ->select();
    }

    /*
     * 获取商品详情数据
     * author chen
     * @param $goods_id   商品ID(必选)
     */
    public function getGoodsDetail()
    {
        $json_arr = $this->Security();
//        $json_arr['goods_id'] = 6;
        if (empty($json_arr['goods_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $goods_info = M('goods')
            ->field('goods_id,goods_name,goods_describe,goods_content,goods_price,goods_price_vip,goods_pic1,goods_pic2,goods_pic3')
            ->find($json_arr['goods_id']);
        $this->backToClient(1,'获取成功',$goods_info);
    }

    /*
     * 获取商品分页数据(可搜索)
     * author chen
     * @param $user_id   用户ID(必选)
     */
    public function getGoodsListData()
    {
        $json_arr = $this->Security();
//        $json_arr['search_name'] = '2';
        if (!empty($json_arr['search_name'])) {
            $where['goods_name'] = array('like','%'.$json_arr['search_name'].'%');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $goods_list_data = $this->getGoodsPageData($p,$pnum,$where);
        $this->backToClient(1,'获取成功',$goods_list_data);
    }

    /*
     * 商品加入购物车
     * author chen
     * @param $goods_cart_user_id   用户ID(必选)
     * @param $goods_id   商品ID(必选)
     */
    public function addGoodsCart()
    {
        $json_arr = $this->Security();
//        $json_arr['goods_cart_user_id'] = 1000028;
//        $json_arr['goods_id'] = 108;
        $json_arr['goods_num'] = empty($json_arr['goods_num']) ? 1 : $json_arr['goods_num'];
        if (empty($json_arr['goods_cart_user_id']) || empty($json_arr['goods_id'])) {
            $this->backToClient(2,'参数不全');
        }
        $goods_cart_model = M('goods_cart');
        $find_res = $goods_cart_model
            ->field('goods_cart_id,goods_cart_user_id,goods_cart_details')
            ->where(array('goods_cart_user_id'=>$json_arr['goods_cart_user_id']))
            ->find();
        //第一次加入购物车
        if(empty($find_res)){
            $str = $json_arr['goods_id'].':'.$json_arr['goods_num'];
            $res = $goods_cart_model->add(array(
                    'goods_cart_user_id'=>$json_arr['goods_cart_user_id'],
                    'goods_cart_details'=>$str,
                )
            );
        }else if(!empty($find_res['goods_cart_details'])){
            //购物车中已经有数据，执行更新操作
            $goods_cart_data = explode(',',$find_res['goods_cart_details']);
            $new_goods_id_array = [];
            foreach ($goods_cart_data as $k => &$v) {
                $new_goods_id_array[] = trim(strstr($v,':',true),':');
            }
            if(in_array($json_arr['goods_id'],$new_goods_id_array)){
                foreach ($goods_cart_data as $key => &$val) {
                    if($json_arr['goods_id'] == trim(strstr($val,':',true),':')){
                        $val = $json_arr['goods_id'].':'.(trim(strstr($val,':'),':') + $json_arr['goods_num']);
                    }
                }
                $find_res['goods_cart_details'] = implode(',',$goods_cart_data);
            }else{
                $find_res['goods_cart_details'] =
                    $json_arr['goods_id'].':'.$json_arr['goods_num'].','.$find_res['goods_cart_details'];
            }
            $res = $goods_cart_model->save($find_res);
        }else if(empty($find_res['goods_cart_details'])){
            $str = $json_arr['goods_id'].':'.$json_arr['goods_num'];
            $res = $goods_cart_model->save(array(
                    'goods_cart_id'=>$find_res['goods_cart_id'],
                    'goods_cart_user_id'=>$json_arr['goods_cart_user_id'],
                    'goods_cart_details'=>$str,
                )
            );
        }
        if($res){
            $this->backToClient(1,'加入购物车成功');
        }else{
            $this->backToClient(3,'加入购物车失败');
        }
    }

    /*
     * 获取购物车列表数据
     * author chen
     * @param $goods_cart_user_id   用户ID(必选)
     */
		 public function getGoodsCartListData()
     {
         $json_arr = $this->Security();
 //        $json_arr['goods_cart_user_id'] = 1000000;
         if(empty($json_arr['goods_cart_user_id'])){
             $this->backToClient(2,'参数不全');
         }
         $goods_cart_arr = M('goods_cart')
 						->field('goods_cart_details')
             ->where(array('goods_cart_user_id'=>$json_arr['goods_cart_user_id']))
             ->find();
         if(!empty($goods_cart_arr)){
             $goods_cart_arr['goods_cart_details'] = explode(',',$goods_cart_arr['goods_cart_details']);
             $goods_id_array = $goods_id_num_array = [];
             foreach ($goods_cart_arr['goods_cart_details'] as $k => &$v) {
                 $goods_id_array[] = trim(strstr($v,':',true),':');
                 $goods_id_num_array[] = array(
                     'goods_id'=>trim(strstr($v,':',true),':'),
                     'goods_num'=>trim(strstr($v,':'),':'),
                 );
             }
             //查询商品信息
             $goods_where['goods_id'] = array('in',$goods_id_array);
 //            var_dump($goods_id_num_array);
             $goods_data = M('goods')
                 ->field('goods_id,goods_name,goods_describe,goods_price,goods_price_vip,goods_pic1,goods_display')
                 ->where($goods_where)
                 ->select();
             $goods_data_list = [];
             foreach ($goods_id_num_array as $k => $v) {
                 foreach ($goods_data as $val) {
                     if($v['goods_id'] == $val['goods_id']){
                         $val['goods_num'] = $v['goods_num'];
                         $goods_data_list[] = $val;
                     }
                 }
             }
 //            var_dump($goods_data_list);
             $this->backToClient(1,'获取成功',$goods_data_list);
         }else{
 					//手动生成一条购物车记录
 					M('goods_cart')->add(['goods_cart_user_id'=>$json_arr['goods_cart_user_id']]);
           $this->backToClient(1,'没有数据');
         }
     }

		 /**
			*判断用户购物车中是否有商品
			*@author chen
			*@param user_id 用户ID(必选)
			*
			*/
			public function getGoodsCartHas()
			{
				$json_arr = $this->Security();
	//        $json_arr['goods_cart_user_id'] = 1000000;
				if(empty($json_arr['user_id'])){
						$this->backToClient(2,'参数不全');
				}
				$res = M('goods_cart')
				->field('goods_cart_user_id,goods_cart_details')
				->where(['goods_cart_user_id'=>$json_arr['user_id']])
				->find();
				if(!empty($res['goods_cart_details'])){
					$this->backToClient(1,'有商品',1);
				}else{
					$this->backToClient(1,'没有商品',0);
				}
			}

    /*
     * 清空购物车
     * @param $goods_cart_user_id   用户ID(必选)
     */
    public function clearUserGoodsCart()
    {
        $json_arr = $this->Security();
//        $json_arr['goods_cart_user_id'] = 1000022;
        if(empty($json_arr['goods_cart_user_id'])){
            $this->backToClient(2,'参数不全');
        }
        $res = M('goods_cart')
            ->where(array('goods_cart_user_id'=>$json_arr['goods_cart_user_id']))
            ->save(array('goods_cart_details'=>''));
        if($res){
            $this->backToClient(1,'清空成功');
        }else{
            $this->backToClient(3,'清空失败');
        }
    }

    /*
     * 删除购物车中商品，可删除多个商品
     * author chen
     * @param $goods_cart_user_id   用户ID(必选)
     * @param $str   要删除的商品ID,多个商品时中间用逗号隔开(必选)
     */
    public function delUserGoodsCart()
    {
        $json_arr = $this->Security();
//        $json_arr['goods_cart_user_id'] = 1000000;
//        $json_arr['str'] = '3,5,6,7';
        if(empty($json_arr['goods_cart_user_id']) || empty($json_arr['str'])){
            $this->backToClient(2,'参数不全');
        }
        $del_goods_id_array = explode(',',$json_arr['str']);
        $goods_cart_model = M('goods_cart');
        $goods_cart_details = $goods_cart_model
            ->where(array('goods_cart_user_id'=>$json_arr['goods_cart_user_id']))
            ->getField('goods_cart_details');
        if(strpos($goods_cart_details,',')){
            $arr_goods_cart_details = explode(',',$goods_cart_details);
            foreach ($arr_goods_cart_details as $k => $v) {
                foreach ($del_goods_id_array as $val) {
                    if(trim(strstr($v,':',true),':') == $val){
                        unset($arr_goods_cart_details[$k]);
                    }
                }
            }
            if(!empty($arr_goods_cart_details)){
                $goods_cart_details = implode(',',$arr_goods_cart_details);
            }else{
                $goods_cart_details = '';
            }
        }else{
            $goods_cart_details = '';
        }
        $res = M('goods_cart')
            ->where(array('goods_cart_user_id'=>$json_arr['goods_cart_user_id']))
            ->save(array('goods_cart_details'=>$goods_cart_details));
        if($res){
            $this->backToClient(1,'删除成功');
        }else{
            $this->backToClient(3,'删除失败');
        }
    }

    /*
     * 点播页面数据
     * author chen
     */
    public function getVodIndexData()
    {
        $json_arr = $this->Security();
        /**热门推荐数据*/
        $hot_recommend_ids = $this->getHotRecommendData(2);
//        var_dump($hot_recommend_ids);
        //查询点播数据
        $where['vod_id'] = array('in',$hot_recommend_ids);
        $where['vod_display'] = array('eq',1);
        $hot_recommend_data = M('vod')
            ->field('vod_id,vod_name,vod_img,vod_playing_time')
            ->where($where)
            ->select();
        //按照点播推荐对数据进行重新排序
        $new_hot_recommend_data = [];
        foreach ($hot_recommend_ids as $v) {
            foreach ($hot_recommend_data as $val) {
                if($v == $val['vod_id']){
                    $new_hot_recommend_data[] = $val;
                }
            }
        }
        $hot_recommend_data = $new_hot_recommend_data;
//        var_dump($hot_recommend_data);
        /**点播分类数据*/
        $vod_cate_data = M('vod_cate')
            ->field('vod_cate_id,vod_cate_name,vod_cate_img')
            ->where(array('vod_cate_display'=>1))
            ->order('vod_cate_createtime')
            ->select();
//        var_dump($vod_cate_data);
        $this->backToClient(1,'获取成功',array(
            'hot_vod_data'=>$hot_recommend_data,
            'vod_cate_data'=>$vod_cate_data,
            )
        );
    }

		/*
     * 获取点播详情数据
     * author chen
     * @param user_id 当前登录用户ID(必填)
     * @param vod_id 点播ID(必填)
     */
    public function getVodDetailData()
    {
       $json_arr = $this->Security();
      //  $json_arr['user_id'] = 1000021;
      //  $json_arr['vod_id'] = 5;
        if(empty($json_arr['vod_id']) || empty($json_arr['user_id'])){
            $this->backToClient(2,'参数不全');
        }
				$is_vip = 0;
				//判断用户身份
				$user_info = $this->getPriUserInfo($json_arr['user_id']);
				if($user_info['user_role'] == 3){
					$is_vip = 1;
				}else if($user_info['user_role'] == 2 && $user_info['user_vip_valid_time'] > time()){
					$is_vip = 1;
				}else if($user_info['user_role'] == 2 && $user_info['user_vip_valid_time'] <= time()){
					//VIP已过期，将VIP用户更新为普通用户
					M('user')->save(['user_id'=>$user_info['user_id'],'user_role'=>1]);
				}
				if($user_info['user_display'] != 1){
					$is_vip = 0;
				}
        $vod_info = M('vod')
            ->field('vod_id,vod_name,vod_describe,vod_img,vod_url')
            ->find($json_arr['vod_id']);
        if($vod_info){
						$vod_info['is_vip'] = $is_vip;
            $this->backToClient(1,'获取成功',$vod_info);
        }else{
            $this->backToClient(3,'获取成功',$vod_info);
        }
    }

    /*
     * 获取点播分页数据(带搜索)
     * author chen
     * @param $search_name 要搜索的点播名称(可选)
     * @param $vod_cate_id 要搜索的点播分类ID(可选)
     * @param $p 页码(可选默认为1)
     * @param $pnum 第页显示条数(可选默认为10)
     */
    public function getVodPageData()
    {
        $json_arr = $this->Security();
//        $json_arr['search_name'] = '1';
        $json_arr['vod_cate_id'] = 1;
        if(!empty($json_arr['search_name'])){
            $where['v.vod_name'] = array('like','%'.$json_arr['search_name'].'%');
        }
        if($json_arr['vod_cate_id'] > 0){
            $where['vc.vod_cate_id'] = array('eq',$json_arr['vod_cate_id']);
        }
        $where['v.vod_display'] = array('eq',1);
        $where['vc.vod_cate_display'] = array('eq',1);
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        //获取数据
        $data = M('vod')
            ->alias('v')
            ->field('vod_id,vod_name,vod_img,vod_view_num,vod_playing_time,vod_cate_id,vod_cate_name')
            ->join('left join bx_vod_cate vc on v.vod_cateid=vc.vod_cate_id')
            ->where($where)
            ->order('v.vod_createtime desc')
            ->page($p,$pnum)
            ->select();
        $this->backToClient(1,'获取成功',$data);
    }

    /*
     * 名师汇页面数据
     * author chen
     */
    public function getTeacherIndexData()
    {
       $json_arr = $this->Security();
        /**获取轮播图片数据*/
        $banner_data = $this->getBannerData(1);
//        var_dump($banner_data);
        /**获取名师分类下所有讲师*/
        //获取所有名师分类数据
        $teacher_cate_data = M('teacher_cate')
            ->field('teacher_cate_id,teacher_cate_name')
            ->where(array('teacher_cate_display'=>1))
            ->order('teacher_cate_sort desc,teacher_cate_id')
            ->select();
        foreach ($teacher_cate_data as &$v) {
            $v['teacher_data'] = $this->getTeacherCateData($v['teacher_cate_id'],[],1,3);
        }
//        var_dump($teacher_cate_data);
        $return_data = [
            'banner_data'=>$banner_data,
            'teacher_data'=>$teacher_cate_data,
        ];
        $this->backToClient(1,'获取成功',$return_data);
    }
    //获取指定讲师分类下讲师数据
    public function getTeacherCateData($cate_id=0,$where=array(),$p=1,$pnum=10)
    {
        if($cate_id > 0){
            $where['t.teacher_cateid'] = array('eq',$cate_id);
        }
        $where['t.teacher_display'] = array('eq','1');
        $where['u.user_display'] = array('eq','1');
        $where['u.user_role'] = array('eq','3');//必须是讲师身份
        return M('teacher')
            ->alias('t')
            ->field('t.teacher_cateid,u.user_id,u.user_name,user_avatar,u.user_autograph')
            ->join('left join bx_user u on t.teacher_userid=u.user_id')
            ->where($where)
            ->page($p,$pnum)
            ->order('teacher_createtime desc')
            ->select();
    }

    /*
     * 获取讲师热门搜索数据
     *  @author chen
     */
		 public function getHotTeacherNameData()
     {
         $json_arr = $this->Security();
         $data = M('teacher_hot_search')
         ->order('teacher_hot_search_createtime desc')
         ->getField('teacher_hot_search_username',true);
         $this->backToClient(1,'获取成功',$data);
     }

		 /*
      * 获取讲师分页数据(带搜索)
      * author chen
      * @param user_id 当前登录用户ID(必填)
      * @param search_name 要搜索的讲师名字(可选)
      * @param teacher_cate_id 要搜索的讲师分类ID(可选)
      * @param p 页码 默认1(可选)
      * @param pnum 每页条数 默认10(可选)
      */
     public function getTeacherPageData()
     {
         $json_arr = $this->Security();
         // $json_arr['user_id'] = '1000028';//当前登录用户ID，用以判断用户是否关注该讲师
         // $json_arr['search_name'] = '1';//要搜索的讲师名称
         // $json_arr['teacher_cate_id'] = '1';//讲师分类ID
         if(!empty($json_arr['search_name'])){
             $where['u.user_name'] = array('like','%'.$json_arr['search_name'].'%');
             $where['t.teacher_userid'] = array('neq',$json_arr['user_id']);
         }
         $p = (!empty($json_arr['p']))?$json_arr['p']:1;
         $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
         $teacher_list_data = $this->getTeacherCateData($json_arr['teacher_cate_id'],$where,$p,$pnum);
         //查询当前用户关注的所有讲师
         $attention_data = $this->getUserAttentionData($json_arr['user_id']);
         //判断搜索的讲师数据中，用户关注的讲师
         foreach ($teacher_list_data as &$val) {
             $val['is_attention'] = 0;
             foreach ($attention_data as $v) {
                 if($val['user_id'] == $v['user_attention_by_userid']){
                     $val['is_attention'] = 1;
                 }
             }
         }
         $this->backToClient(1,'获取成功',$teacher_list_data);
     }

    /*
     * 关注讲师
     * author chen
     * @param user_attention_userid 关注用户ID(必选)
     * @param user_attention_by_userid 被关注讲师用户ID(必选)
     */
    public function setAttentionTeacher()
    {
        $json_arr = $this->Security();
        // $json_arr['user_attention_userid'] = 1000022;
        // $json_arr['user_attention_by_userid'] = 1000025;
        $json_arr['user_attention_createtime'] = time();
        if(empty($json_arr['user_attention_userid']) || empty($json_arr['user_attention_by_userid'])){
            $this->backToClient(2,'参数不全');
        }
				if($json_arr['user_attention_userid'] == $json_arr['user_attention_by_userid']){
						$this->backToClient(3,'参数传递错误');
				}
				$user_attention_model = M('user_attention');
				//判断是否已经关注过
				$has_data = $user_attention_model
					->where(
							array(
							'user_attention_userid'=>$json_arr['user_attention_userid'],
							'user_attention_by_userid'=>$json_arr['user_attention_by_userid'],
							)
					)
					->count();
				if($has_data > 0){
					$this->backToClient(4,'不能重复关注');
				}
        //判断被关注人是否是关注人粉丝
        $fans_res = $user_attention_model
            ->where(
                array(
                'user_attention_userid'=>$json_arr['user_attention_by_userid'],
                'user_attention_by_userid'=>$json_arr['user_attention_userid'],
                )
            )
            ->count();
        if($fans_res == 1){
            $json_arr['user_attention_fans'] = 1;
            $save_fans_res = $user_attention_model
                ->where(
                    array(
                        'user_attention_userid'=>$json_arr['user_attention_by_userid'],
                        'user_attention_by_userid'=>$json_arr['user_attention_userid'],
                    )
                )
                ->save(array('user_attention_fans'=>1));
        }
        $add_res = $user_attention_model
            ->add($json_arr);
        $save_user_funscount_res = M('user')
            ->where(array('user_id'=>$json_arr['user_attention_userid']))
            ->setInc('user_funscount');
        $this->backToClient(1,'关注成功');
    }

    //查询用户所有关注数据
    private function getUserAttentionData($user_id)
    {
        return M('user_attention')
            ->field('user_attention_createtime',true)
            ->where(array('user_attention_userid'=>$user_id))
            ->order('user_attention_createtime desc')
            ->select();
    }

		/*
     * 查询用户所有关注列表数据
     * @author chen
     * @user_id 用户ID(必填)
     */
    public function getUserAttentionListData()
    {
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1000021;
        if(empty($json_arr['user_id'])){
            $this->backToClient(2,'参数不全');
        }
        /**查询用户所有关注数据*/
        $attention_data = $this->getUserAttentionData($json_arr['user_id']);
      //  var_dump($attention_data);
      	$return_data = [];
      	if(!empty($attention_data)){
					//所有被关注用户ID
	        $attention_by_user_id_arr = [];
	        foreach ($attention_data as $value) {
	            $attention_by_user_id_arr[] = $value['user_attention_by_userid'];
	        }
	        $by_where['user_display'] = array('eq',1);
	        $by_where['user_role'] = array('eq',3);
	        $by_where['user_id'] = array('in',$attention_by_user_id_arr);
	        $attention_by_user_id_data = M('user')
	            ->field('user_id,user_name,user_avatar,user_autograph')
	            ->where($by_where)
	            ->select();
	      //  var_dump($attention_by_user_id_data);
	        //处理数据让其按照关注数据排序并处理返回互相关注和关注的数据
	        $return_data = ['fans_attention_data'=>[],'attention_data'=>[]];
	        foreach ($attention_data as $value) {
	            foreach ($attention_by_user_id_data as $v) {
	                if($value['user_attention_by_userid'] == $v['user_id']){
	                    if($value['user_attention_fans'] == 1){
	                        $return_data['fans_attention_data'][] = $v;
	                    }else{
	                        $return_data['attention_data'][] = $v;
	                    }
	                }
	            }
	        }
				}
//        var_dump($return_data);
        $this->backToClient(1,'获取成功',$return_data);
    }

    /*
     * 取消关注
     * @author chen
     * @param user_id 当前登录用户ID(必选)
     * @param by_user_id 被关注讲师用户ID(必选)
     */
    public function delAttention()
    {
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1000022;
        // $json_arr['by_user_id'] = 1000021;
        if(empty($json_arr['user_id']) || empty($json_arr['by_user_id'])){
            $this->backToClient(2,'参数不全');
        }
        //判断被关注人是否是关注人粉丝
        $user_attention_model = M('user_attention');
        $fans_res = $user_attention_model
            ->where(
                array(
                    'user_attention_userid'=>$json_arr['by_user_id'],
                    'user_attention_by_userid'=>$json_arr['user_id'],
                )
            )
            ->count();
        if($fans_res == 1){
            $user_attention_model
                ->where(
                    array(
                        'user_attention_userid'=>$json_arr['by_user_id'],
                        'user_attention_by_userid'=>$json_arr['user_id'],
                    )
                )
                ->save(array('user_attention_fans'=>0));
        }
        $del_res = $user_attention_model
            ->where(
                array(
                    'user_attention_userid'=>$json_arr['user_id'],
                    'user_attention_by_userid'=>$json_arr['by_user_id'],
                )
            )
            ->delete();
        //用户关注数减少
        M('user')->where(array('user_id'=>$json_arr['user_id']))->setDec('user_funscount');
        $this->backToClient(1,'取消关注成功');

    }

		/**
     *讲师主页
     *@author  chen
     *@param user_id 当前登录用户ID(必选)
     *@param by_user_id 要查询的讲师用户ID(必选)
     */
    public function getTeacherHomepageData()
    {
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1000021;
        // $json_arr['by_user_id'] = 1000022;
        if(empty($json_arr['user_id']) || empty($json_arr['by_user_id'])){
            $this->backToClient(2,'参数不全');
        }
        $return_data = M('teacher')
        ->alias('t')
        ->field('u.user_id,u.user_name,u.user_avatar,t.teacher_cateid,t.teacher_introduce_img,t.teacher_introduce_video,t.teacher_describe,t.teacher_introduce')
        ->join('left join bx_user u on t.teacher_userid=u.user_id')
        ->where(array('u.user_id'=>$json_arr['by_user_id']))
        ->find();
        $is_attention = M('user_attention')
        ->where(array(
            'user_attention_userid'=>$json_arr['user_id'],
            'user_attention_by_userid'=>$json_arr['by_user_id'],
            ))
        ->count();
        $return_data['is_attention'] = 0;
        if($is_attention > 0){
            $return_data['is_attention'] = 1;
        }
        // var_dump($return_data);
        /**用户直播及回放数据*/
        $live_doing_data = M('teacher_live')
        ->field('teacher_live_id,teacher_live_title,teacher_live_img,teacher_live_money')
        ->where(array(
            'teacher_live_userid'=>$json_arr['by_user_id'],
            'teacher_live_status'=>1,
            ))
        ->find();
				//判断用户是否购买该直播课
				if(!empty($live_doing_data)){
					$is_buy_doing_live = $this->isBuyTeacherVideo($json_arr['user_id'],$live_doing_data['teacher_live_id'],3);
					$live_doing_data['is_buy'] = 0;
					if($is_buy_doing_live == 1){
						$live_doing_data['is_buy'] = 1;
					}
				}
				if($json_arr['user_id'] == $json_arr['by_user_id']){
					$live_doing_data['is_self'] = 1;
				}else {
					$live_doing_data['is_self'] = 0;
				}
				$return_data['live_doing_data'] = $live_doing_data;
        $this->backToClient(1,'获取成功',$return_data);
    }

		/**
		 * [getTeacherLivePlaybackData 获取用户直播回放视频数据 带分页]
		 * @author chen
		 * @param user_id 当前登录用户ID(必选)
		 * @param by_user_id 需查询的讲师用户ID(必选)
		 * @param p 页码 默认1(可选)
		 * @param pnum 每页显示条数 默认10(可选)
		 */
		public function getTeacherLivePlaybackData()
		{
			$json_arr = $this->Security();
			// $json_arr['user_id'] = 1000021;
			// $json_arr['by_user_id'] = 1000021;
			if(empty($json_arr['user_id']) || empty($json_arr['by_user_id'])){
					$this->backToClient(2,'参数不全');
			}
			$p = (!empty($json_arr['p']))?$json_arr['p']:1;
			$pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
			$data = $this->backTeacherPlaybackData($json_arr['by_user_id'],$json_arr['user_id'],1,4);
			if(!empty($data)){
				foreach ($data as $key => &$value) {
					if($json_arr['user_id'] == $json_arr['by_user_id']){
						$value['is_self'] = 1;
					}else {
						$value['is_self'] = 0;
					}
				}
			}
			$this->backToClient(1,'获取成功',$data);
		}

		//获取用户直播后回放视频数据
    private function backTeacherPlaybackData($user_id,$buy_userid,$p=1,$pnum=10)
    {
        return M('teacher_live_playback')
            ->alias('lp')
            ->field('l.teacher_live_title,l.teacher_live_img,l.teacher_live_playing_time,lp.teacher_live_playback_id,lp.teacher_live_playback_userid,lp.teacher_live_playback_money,lp.teacher_live_playback_url,m.user_money_transact_id')
            ->join('left join bx_teacher_live l on l.teacher_live_id=lp.teacher_live_playback_liveid')
            ->join('left join bx_user_money_transact m on l.teacher_live_id=m.user_money_transact_tableid and m.user_money_transact_type=4 and m.user_money_transact_userid='.$buy_userid)
            ->where(array(
                'lp.teacher_live_playback_is'=>2,
                'l.teacher_live_userid'=>$user_id,
            ))
            ->page($p,$pnum)
            ->order('lp.teacher_live_playback_createtime desc')
            ->select();
    }

		/**
		 * [isUserSale 判断用户是否购买讲师直播或讲师直播回看]
		 * @param  [type]  $user_id  [购买用户ID]
		 * @param  [type]  $table_id [视频ID]
		 * @param  [type]  $type     [description]
		 * 1充值兑换的金币
		 * 2被介绍人购物时奖励给介绍人的金币
		 * 3用户观看讲师直播消费的金币
		 * 4用户观看讲师直播回放消费的金币
		 * 5讲师直播收入金币
		 * 6讲师回放收入金币
		 * @return boolean           [购买过返回true 否则 false]
		 */
		public function isBuyTeacherVideo($user_id,$table_id,$type)
		{
			return M('user_money_transact')
				->where(
					[
						'user_money_transact_userid'=>$user_id,
						'user_money_transact_tableid'=>$table_id,
						'user_money_transact_type'=>$type,
					]
					)
				->count();
		}

    /**
     *讲师开始直播
     *@author chen
     *@param teacher_live_userid 讲师用户ID(必选)
     *@param teacher_live_title 直播标题(必选)
     *@param teacher_live_img 直播背景图片(必选)
     *@param teacher_live_money 观看金币数(必选)
     *
     */
    public function teacherStartLive()
    {
        $json_arr = $this->Security();
        // $json_arr['teacher_live_userid'] = 1000022;
        // $json_arr['teacher_live_title'] = '2017-5保险投资';
        // $json_arr['teacher_live_img'] = 'http://www.baidu.com/img1.png';
        // $json_arr['teacher_live_money'] = 5;
        if(empty($json_arr['teacher_live_userid']) || empty($json_arr['teacher_live_title'])){
            $this->backToClient(2,'参数不全');
        }
        //判断用户身份
        $user_info = $this->getPriUserInfo($json_arr['teacher_live_userid']);
        if(empty($user_info) ||
            $user_info['user_display'] != 1 ||
            $user_info['user_role'] != 3 ||
            $user_info['user_vip_valid_time'] <= time()
        ){
            $this->backToClient(3,'用户被禁用或者不是讲师身份');
        }
        $json_arr['teacher_live_createtime'] = time();
        $add_res = M('teacher_live')->add($json_arr);
        if($add_res){
            //将用户之前的直播全部关闭
            $live_status_res = M('teacher_live')
                ->where([
                    'teacher_live_userid'=>['eq',$json_arr['teacher_live_userid']],
                    'teacher_live_id'=>['neq',$add_res],
                ])
                ->save(['teacher_live_status'=>0]);
            //给关注该讲师的用户发送消息及极光推送消息
            $user_arr = M('user_attention')
                ->where(['user_attention_by_userid'=>$json_arr['teacher_live_userid']])
                ->getField('user_attention_userid',true);
            if(!empty($user_arr)){
                //给用户发送消息
                sendUserMsg($user_arr,$user_info['user_name'],3);
                //给用户推送消息(极光)
                $receive_value = [];
                foreach ($user_arr as $val) {
                    $receive_value[] = 'user'.$val;
                }
                push(['alias'=>$receive_value],'您关注的讲师'.$user_info['user_name'].'开播了',C('JPUSH_USER'));
            }
            $this->backToClient(1,'讲师直播成功',$add_res);
        }else{
            $this->backToClient(3,'讲师直播失败');
        }
    }

    /**
     *获取用户直播后所有回放视频（客户端调getTeacherLivePlaybackData()方法）
     *@author chen
     *@param  user_id 用户ID(必选)
     *
     */
    public function getUserLivePlaybackData()
    {
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1000021;
        if(empty($json_arr['user_id'])){
            $this->backToClient(2,'参数不全');
        }
        $data = M('teacher_live_playback')
        ->alias('lp')
        ->field('lp.teacher_live_playback_id,lp.teacher_live_playback_url,l.teacher_live_playing_time,l.teacher_live_title,l.teacher_live_img')
        ->join('left join bx_teacher_live l on lp.teacher_live_playback_liveid=l.teacher_live_id')
        ->where(array('lp.teacher_live_playback_userid'=>$json_arr['user_id']))
        ->order('lp.teacher_live_playback_createtime desc')
        ->select();
        $this->backToClient(1,'获取成功',$data);
    }

    /**
     *查询用户会议室审核状态
     *@author chen
     *@param  conference_audit_userid 申请用户ID(必选)
     */
    public function getUserConferenceStatus()
    {
        $json_arr = $this->Security();
        // $json_arr['conference_audit_userid'] = 1000021;
        if(empty($json_arr['conference_audit_userid'])){
            $this->backToClient(2,'参数不全');
        }
        $status = M('conference_audit')
        ->field('conference_audit_status')
        ->where(array('conference_audit_userid'=>$json_arr['conference_audit_userid']))
        ->find();
        if(!empty($status)){
            $this->backToClient(1,'查询成功',$status['conference_audit_status']);
        }else{
            $this->backToClient(1,'查询成功','0');
        }
    }

		/**
     *会议室申请或修改会议室审核信息
     * @author chen
     * @param  conference_audit_userid 申请用户ID(必选)
     * @param  conference_audit_username 申请用户姓名(必选)
     * @param  conference_audit_code 申请用户姓名身份证号(必选)
     * @param  conference_audit_img1 申请用户身份证正面照片(必选)
     * @param  conference_audit_img2 申请用户身份证反面照片(必选)
     * @param  conference_audit_mobile 申请用户手机号(必选)
     * @param  conference_audit_email 申请用户邮箱(必选)
     * @param  conference_audit_address 申请用户地址(必选)
     */
    public function setUserConferenceInfo()
    {
        $json_arr = $this->Security();
        // $json_arr['conference_audit_userid'] = 1000021;
        // $json_arr['conference_audit_username'] = '张林111';
        // $json_arr['conference_audit_code'] = '987654321098x';
        // $json_arr['conference_audit_img1'] = 'http://www.baidu1111.com';
        // $json_arr['conference_audit_img2'] = 'http://www.baidu2222.com';
        // $json_arr['conference_audit_mobile'] = '18812345678';
        // $json_arr['conference_audit_email'] = '88889999@qq.com';
        // $json_arr['conference_audit_address'] = '北京海淀上地1111';
        if(empty($json_arr['conference_audit_userid']) || empty($json_arr['conference_audit_username']) || empty($json_arr['conference_audit_code']) || empty($json_arr['conference_audit_img1']) || empty($json_arr['conference_audit_img2']) || empty($json_arr['conference_audit_mobile']) || empty($json_arr['conference_audit_email']) || empty($json_arr['conference_audit_address'])){
            $this->backToClient(2,'参数不全');
        }
				//判断用户之前是否提交过信息
				$model = M('conference_audit');
				$find_res = $model
					->field('conference_audit_id')
					->where(['conference_audit_userid'=>$json_arr['conference_audit_userid']])
					->find();
				$json_arr['conference_audit_createtime'] = time();
				if(!empty($find_res)){
					$json_arr['conference_audit_id'] = $find_res['conference_audit_id'];
					$res = $model->save($json_arr);
				}else{
	        $res = $model->add($json_arr);
				}
        if($res){
            $this->backToClient(1,'成功');
        }else{
            $this->backToClient(3,'失败');
        }
    }

		/**
 * [获取讲师审核信息]   未用
 * @author chen
 *@param user_id 用户ID(必选)
 *
 */
    public function getConferenceRoomInfo()
    {
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1000021;
        if(empty($json_arr['user_id'])){
            $this->backToClient('参数不全');
        }
        $data = M('conference_audit')
            ->field('conference_audit_createtime,conference_audit_updatetime',true)
            ->where(['conference_audit_userid'=>$json_arr['user_id']])
            ->find();
        $this->backToClient(1,'查询成功',$data);
    }

    /**
     *开始会议直播
     *@author chen
     *@param  conference_room_userid 开通会议室用户ID(必选)
     *@param  conference_room_img 会议室背景图片(可选)
     *@param  conference_room_title 会议室标题(必选)
     *@param  conference_room_pwd 会议室密码(必选)
     *
     */
    public function createConferenceRoom()
    {
        $json_arr = $this->Security();
        // $json_arr['conference_room_userid'] = 1000021;
        // $json_arr['conference_room_title'] = '北京众网科技';
        // $json_arr['conference_room_pwd'] = 123456;
        if(empty($json_arr['conference_room_userid']) || empty($json_arr['conference_room_title']) || empty($json_arr['conference_room_pwd'])){
            $this->backToClient(2,'参数不全');
        }
        //判断用户身份
        $user_info = $this->getPriUserInfo($json_arr['conference_room_userid']);
        if(empty($user_info) ||
            $user_info['user_display'] != 1 ||
            $user_info['user_conference_role'] != 1 ||
            $user_info['user_conference_valid_time'] <= time()
        ){
            $this->backToClient(3,'用户被禁用或者没有开通会议权限');
        }
        $json_arr['conference_room_pwd'] = trim($json_arr['conference_room_pwd']);
        $json_arr['conference_room_createtime'] = time();
        //判断用户之前是否开过会议
        $room_model = M('conference_room');
        $has_res = $room_model
            ->field('conference_room_id')
            ->where(['conference_room_userid'=>$json_arr['conference_room_userid']])
            ->find();
        if($has_res['conference_room_id'] > 0){
            $id = $json_arr['conference_room_id'] = $has_res['conference_room_id'];
            $json_arr['conference_room_status'] = 1;
            $res = $room_model->save($json_arr);
        }else{
            $id = $res = $room_model->add($json_arr);
        }
        if($res){
            $this->backToClient(1,'开通成功',$id);
        }else{
            $this->backToClient(4,'开通失败');
        }
    }

    /**
     *获取会议室列表数据
     *@author chen
     *
     */
    public function getConferenceRoomListData()
    {
        $json_arr = $this->Security();
        $data = M('conference_room')
				->alias('r')
        ->field('r.conference_room_id,r.conference_room_userid,r.conference_room_title,r.conference_room_img,r.conference_room_status,u.user_conference_room')
				->join('left join bx_user u on r.conference_room_userid=u.user_id')
				->where(['u.user_conference_role'=>1])
        ->order('r.conference_room_createtime desc')
        ->select();
        $this->backToClient(1,'获取成功',$data);
    }

    /**
     *用户进入会议室
     *@author  chen
     *@param user_id 用户ID(必选)
     *@param conference_room_id 会议室ID(必选)
     *@param pwd 会议室密码(必选)
     *
     */
    public function signInConferenceRoom()
    {
        $json_arr = $this->Security();
        // $json_arr['user_id'] = '1000022';
        // $json_arr['conference_room_id'] = 2;
        // $json_arr['pwd'] = 123456;
        if(empty($json_arr['user_id']) || empty($json_arr['conference_room_id']) || empty($json_arr['pwd'])){
            $this->backToClient(2,'参数不全');
        }
        $room_info = M('conference_room')
        ->field('conference_room_pwd')
        ->find($json_arr['conference_room_id']);
        if(!empty($room_info)){
            if($room_info['conference_room_pwd'] == trim($json_arr['pwd'])){
                $this->backToClient(1,'验证成功');
            }else{
                $this->backToClient(4,'房间密码不正确');
            }
        }else{
            $this->backToClient(3,'没有此房间数据');
        }
    }

		/**
		 *获取用户状态及金币数 VIP有效时间
		 *
		 */
		 private function getPriUserInfo($user_id)
		 {
			 return M('user')
			 ->field('user_id,user_role,user_conference_role,user_available_money,user_vip_valid_time,user_conference_valid_time,user_display,user_conference_room')
			 ->find($user_id);
		 }

		 /**
		  * 判断讲师是否直播中，直播中心跳时间是否过期，过期时，调整直播间状态为已停止直播
		  * @return boolean [description]
		  */
		 private function isLiveHeartbeatEndtimeExpire($live_id)
		 {
			 $find_info = M('teacher_live')
				 ->field('teacher_live_money,teacher_live_heartbeat_endtime,teacher_live_status')
				 ->find($live_id);
			 if(!empty($find_info) && $find_info['teacher_live_status'] == 1){
				 if($find_info['teacher_live_heartbeat_endtime'] <= time()){
					 //直播心跳已停止
					 $save_res = M('teacher_live')
					 	->save(['teacher_live_id'=>$live_id,'teacher_live_status'=>0]);
						if(!$save_res){
							M('teacher_live')
	 					 	->save(['teacher_live_id'=>$live_id,'teacher_live_status'=>0]);
						}
						return false;
				 }else {
				 	return $find_info;
				 }
			 }else {
			 	return false;//不存在该直播间或已停止直播
			 }
		 }

		/**
		 * 判断用户是否购买该讲师直播
		 * @author chen
		 * @param user_id 用户ID(必选)
		 * @param teacher_live_id 讲师正在直播的房间ID(必选 非房间号)
		 */
		public function signInTeacherLiveRoom()
		{
			$json_arr = $this->Security();
			// $json_arr['user_id'] = 1000021;
			// $json_arr['teacher_live_id'] = 5;
			if(empty($json_arr['user_id']) || empty($json_arr['teacher_live_id'])){
				$this->backToClient(2,'参数不全');
			}
			//判断该视频是否在直播中或心跳是否已停止
			$live_status = $this->isLiveHeartbeatEndtimeExpire($json_arr['teacher_live_id']);
			if($live_status){
				// 判断用户是否被禁用
				$user_info = $this->getPriUserInfo($json_arr['user_id']);
				if(!empty($user_info) && $user_info['user_display'] == 1){
					// 判断用户是否购买过该直播
					if($live_status['teacher_live_money'] > 0){
						$money_log = M('user_money_transact')
						->where([
							'user_money_transact_userid'=>$json_arr['user_id'],
							'user_money_transact_tableid'=>$json_arr['teacher_live_id'],
							'user_money_transact_type'=>3,
						])
						->count();
						if($money_log > 0){
							$this->backToClient(1,'用户已购买该直播,允许进入讲师直播间');
						}else {
							$this->backToClient(5,'用户未购买该直播，不能进入该直播间');
						}
					}else{
						$this->backToClient(1,'该讲师直播无需金币,可直接观看');
					}
				}else {
					$this->backToClient(4,'用户不存在或已被禁用');
				}
			}else{
				$this->backToClient(3,'不存在该直播或直播已结束');
			}
		}

		/**
		 *用户购买讲师本次直播
		 *@author chen
		 *@param user_id 用户ID(必选)
		 *@param teacher_live_id 讲师正在直播的房间ID(必选 非房间号)
		 *
		 */
		 public function userBuyTeacherLive()
		 {
			 $json_arr = $this->Security();
				// $json_arr['user_id'] = 1000023;
				// $json_arr['teacher_live_id'] = 5;
				if(empty($json_arr['user_id']) || empty($json_arr['teacher_live_id'])){
					$this->backToClient(2,'参数不全');
				}
				// 判断用户状态及金币数
				$user_info = $this->getPriUserInfo($json_arr['user_id']);
				if(!empty($user_info) && $user_info['user_display'] == 1){
					$teacher_live_data = M('teacher_live')
						->field('teacher_live_userid,teacher_live_money,teacher_live_title')
						->find($json_arr['teacher_live_id']);
					if(!empty($teacher_live_data)){
						if($json_arr['user_id'] != $teacher_live_data['teacher_live_userid']){
							if($teacher_live_data['teacher_live_money'] > 0){
								// 判断讲师直播间是否还在进行中
								$live_status = $this->isLiveHeartbeatEndtimeExpire($json_arr['teacher_live_id']);
								if(!$live_status){
									$this->backToClient(8,'直播已结束');
								}
								//判断用户是否已购买该直播
								$is_buy = M('user_money_transact')
									->where([
											'user_money_transact_userid'=>$json_arr['user_id'],
											'user_money_transact_tableid'=>$json_arr['teacher_live_id'],
											'user_money_transact_type'=>3,
										])
									->count();
								if($is_buy > 0){
									$this->backToClient(1,'已购买过该直播');
								}
								if($user_info['user_available_money'] >= $teacher_live_data['teacher_live_money']){
									//讲师直播首次收益时需要先生成一条记录
									$teacher_money_log = addTeacherMoneyLog($teacher_live_data['teacher_live_userid'],$json_arr['teacher_live_id'],1);
									if($teacher_money_log){
										$money_res = userMoneyTransactAction($json_arr['user_id'],$teacher_live_data['teacher_live_userid'],$json_arr['teacher_live_id'],$teacher_live_data['teacher_live_title'],$teacher_live_data['teacher_live_money'],3);
										if($money_res){
											$this->backToClient(1,'交易成功');
										}else {
											$this->backToClient(7,'交易失败');
										}
									}else {
										$this->backToClient(6,'交易失败');
									}
								}else {
									$this->backToClient(5,'用户可用金币不足,请充值');
								}
							}else{
								$this->backToClient(1,'该直播间不需要金币即可观看');
							}
						}else {
							$this->backToClient(1,'讲师进入自己的直播间');
						}
					}else {
						$this->backToClient(4,'没找到该直播间数据');
					}
				}else {
					$this->backToClient(3,'用户不存在或已被禁用');
				}
		 }

		 /**
		  * 判断用户是否购买该直播回放视频
		  * @param user_id 用户ID(必选)
		  * @param teacher_live_playback_id 直播回放表ID(必选)
		  * @return [type] [description]
		  */
		 public function signInLivePlayback()
		 {
			 $json_arr = $this->Security();
 		// 	$json_arr['user_id'] = 1000023;
 		// 	$json_arr['teacher_live_playback_id'] = 2;
 			if(empty($json_arr['user_id']) || empty($json_arr['teacher_live_playback_id'])){
 				$this->backToClient(2,'参数不全');
 			}
			// 判断用户是否被禁用
			$user_info = $this->getPriUserInfo($json_arr['user_id']);
			if(!empty($user_info) && $user_info['user_display'] == 1){
				// 判断用户是否点击了自己的直播回放视频
				$live_back_info = M('teacher_live_playback')
					->field('teacher_live_playback_liveid,teacher_live_playback_userid')
					->find($json_arr['teacher_live_playback_id']);
					if(empty($live_back_info)){
						$this->backToClient(5,'不存在该直播回放视频');
					}else if($live_back_info['teacher_live_playback_userid'] == $json_arr['user_id']){
						$this->backToClient(1,'讲师查看自己的直播回放视频');
					}else if($live_back_info['teacher_live_playback_liveid'] > 0){
						//判断该用户是否购买该直播回放
						$transact_log = M('user_money_transact')
							->where([
								'user_money_transact_userid'=>$json_arr['user_id'],
								'user_money_transact_tableid'=>$live_back_info['teacher_live_playback_liveid'],
								'user_money_transact_type'=>4,
							])
							->count();
						if($transact_log > 0){
							$this->backToClient(1,'用户已购买该直播回放');
						}else {
							$this->backToClient(4,'未购买该直播回放,请购买');
						}
					}else{
						$this->backToClient(6,'未知错误');
					}
			}else {
				$this->backToClient(3,'用户不存在或已被禁用');
			}
		 }

		 /**
		  * 用户购买直播回放视频
		  * @author chen
		  * @return user_id 用户ID(必选)
		  * @return teacher_live_playback_id 直播回放视频ID(必选)
		  *
		  */
		 public function userBuyTeacherLivePlayback()
		 {
				 $json_arr = $this->Security();
				// 	$json_arr['user_id'] = 1000023;
				// 	$json_arr['teacher_live_playback_id'] = 2;
	 			if(empty($json_arr['user_id']) || empty($json_arr['teacher_live_playback_id'])){
	 				$this->backToClient(2,'参数不全');
	 			}
				// 判断用户状态及金币数
				$user_info = $this->getPriUserInfo($json_arr['user_id']);
				if(!empty($user_info) && $user_info['user_display'] == 1){
					$teacher_live_data = M('teacher_live_playback')
						->alias('lp')
						->field('lp.teacher_live_playback_userid,lp.teacher_live_playback_money,lp.teacher_live_playback_url,l.teacher_live_title,l.teacher_live_id')
						->join('left join bx_teacher_live l on lp.teacher_live_playback_liveid=l.teacher_live_id')
						->where(['lp.teacher_live_playback_id'=>$json_arr['teacher_live_playback_id']])
						->find();
					if(!empty($teacher_live_data)){
						if(empty($teacher_live_data['teacher_live_playback_url'])){
							$this->backToClient(1,'该直播回放视频未转码完成,请稍等');
						}
						if($json_arr['user_id'] != $teacher_live_data['teacher_live_playback_userid']){
							//判断用户是否已购买该直播回放视频
							$transact_log = M('user_money_transact')
								->where([
									'user_money_transact_userid'=>$json_arr['user_id'],
									'user_money_transact_tableid'=>$teacher_live_data['teacher_live_id'],
									'user_money_transact_type'=>4,
								])
								->count();
							if($transact_log > 0){
								$this->backToClient(1,'已购买该直播回放视频');
							}
							if($teacher_live_data['teacher_live_playback_money'] > 0){
								if($user_info['user_available_money'] >= $teacher_live_data['teacher_live_playback_money']){
									//讲师直播回放首次收益时需要先生成一条记录
									$teacher_money_log = addTeacherMoneyLog($teacher_live_data['teacher_live_playback_userid'],$teacher_live_data['teacher_live_id'],2);
									if($teacher_money_log){
										$money_res = userMoneyTransactAction($json_arr['user_id'],$teacher_live_data['teacher_live_playback_userid'],$teacher_live_data['teacher_live_id'],$teacher_live_data['teacher_live_title'],$teacher_live_data['teacher_live_playback_money'],4);
										if($money_res){
											$this->backToClient(1,'交易成功');
										}else {
											$this->backToClient(7,'交易失败');
										}
									}else {
										$this->backToClient(6,'交易失败');
									}
								}else {
									$this->backToClient(5,'用户可用金币不足,请充值');
								}
							}else{
								$this->backToClient(1,'该直播回放不需要金币即可观看');
							}
						}else {
							$this->backToClient(1,'讲师观看自己的直播回放');
						}
					}else {
						$this->backToClient(4,'没找到该直播回放数据');
					}
				}else {
					$this->backToClient(3,'用户不存在或已被禁用');
				}
		 }

    /**
     * 用户结束直播
     * @author chen
     * @param user_id 用户ID(必选)
     * @param live_type 直播类型 1讲师直播 2会议室直播(必选)
     * @param live_id 讲师直播ID(类型为1时必选)
     * @param live_view_num 讲师直播观看总人数(类型为1时必选)
     * @param live_playing_time 直播播放时长(类型为1时必选)
     *
     */
    public function liveOver()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
//        $json_arr['live_type'] = 1; //1讲师直播 2会议室
//        $json_arr['live_id'] = 1;
//        $json_arr['live_view_num'] = 200000; //讲师直播观看总人数
        if(empty($json_arr['user_id']) || empty($json_arr['live_type'])){
            $this->backToClient(2,'参数不全');
        }
        if($json_arr['live_type'] == 1 && $json_arr['user_id'] > 0 && $json_arr['live_id'] > 0){
            if($json_arr['live_view_num'] > 0){
                $live_save_data = [
                    'teacher_live_id'=>$json_arr['live_id'],
                    'teacher_live_userid'=>$json_arr['user_id'],
                    'teacher_live_view_num'=>$json_arr['live_view_num'],
                    'teacher_live_endtime'=>time(),
                    'teacher_live_status'=>0,
                ];
                $res = M('teacher_live')->save($live_save_data);
                if($res){
                    $this->backToClient(1,'讲师直播关闭成功');
                }else{
                    $this->backToClient(4,'讲师直播关闭失败');
                }
            }else {
                $this->backToClient(3,'参数不全');
            }
        }else if($json_arr['live_type'] == 2 && $json_arr['user_id'] > 0){
            $res = M('conference_room')
                ->where(['conference_room_userid'=>$json_arr['user_id']])
                ->save(['conference_room_status'=>0]);
            if($res){
                $this->backToClient(1,'会议室关闭成功');
            }else {
                $this->backToClient(5,'会议室关闭失败');
            }
        }else {
            $this->backToClient(6,'非法请求');
        }
    }

    /**
     * 保存讲师直播回放
     * @author chen
     * @param user_id 用户ID(必选)
     * @param live_id 讲师直播ID(必选)
     * @param live_money 直播回看所需雨点数(必选)
     *
     */
    public function isSaveLive()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
//        $json_arr['live_id'] = 1;
//        $json_arr['live_money'] = 30; //讲师直播设置回放价格
        if(empty($json_arr['user_id']) || empty($json_arr['live_id']) || empty($json_arr['live_money'])){
            $this->backToClient(2,'参数不全');
        }
        $url = '';//向腾讯云获取直播回放地址
        $add_data = [
            'teacher_live_playback_userid'=>$json_arr['user_id'],
            'teacher_live_playback_liveid'=>$json_arr['live_id'],
            'teacher_live_playback_money'=>$json_arr['live_money'],
            'teacher_live_playback_createtime'=>time(),
        ];
        $add_res = M('teacher_live_playback')->add($add_data);
        //用户直播回放视频数增加
        $user_video_res = M('user')
            ->where(['user_id'=>$json_arr['user_id']])
            ->setInc('user_video');
        $this->backToClient(1,'保存成功');
    }

    /**
     * 更新讲师正在直播时的心跳时间
     * @author chen
     * @param user_id 用户ID(必选)
     * @param teacher_live_id 本次直播主键(必选)
     * @param time 心跳时间(必选)
     */
    public function setTeacherLiveHeartbeatEndtime()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000022;
//        $json_arr['teacher_live_id'] = 5;
//        $json_arr['time'] = 123456789;
        if(empty($json_arr['user_id']) || empty($json_arr['teacher_live_id'])){
            $this->backToClient(2,'参数不全');
        }
        $res = M('teacher_live')
            ->where([
                'teacher_live_id'=>$json_arr['teacher_live_id'],
                'teacher_live_userid'=>$json_arr['user_id'],
                'teacher_live_status'=>1,
            ])->save(['teacher_live_heartbeat_endtime'=>$json_arr['time']]);
        if($res){
            $this->backToClient(1,'更新直播心跳时间成功');
        }else {
            $this->backToClient(3,'写入直播心跳时间失败');
        }
    }

    /**
     * 判断用户是否有未读消息
     * @author chen
     * @param user_id 用户ID(必选)
     */
    public function hasUserMsg()
    {
        $json_arr = $this->Security();
        //  $json_arr['user_id'] = 1000021;
        if(empty($json_arr['user_id'])){
            $this->backToClient('参数不全');
        }
        //查询用户是否有未查看消息
        $map['msg_display'] = 0;
        $map['msg_userid'] = $json_arr['user_id'];
        $res = M('msg')
            ->where($map)
            ->count();
        if($res > 0){
            $this->backToClient(1,'获取成功',1);
        }else {
            $this->backToClient(1,'获取成功',0);
        }
    }

    /**
     * 获取用户消息列表
     * @author chen
     * @param user_id 用户ID(必选)
     * @param p 页码 默认1(可选)
     * @param pnum 每页条数 默认10(可选)
     */
    public function getUserMsgData()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
//        $json_arr['p'] = 1;
//        $json_arr['pnum'] = 10;
        if(empty($json_arr['user_id'])){
            $this->backToClient('参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        //查询用户是否有未查看消息
        $map['msg_userid'] = $json_arr['user_id'];
        $data = M('msg')
            ->field('msg_userid',true)
            ->where($map)
            ->order('msg_createtime desc')
            ->page($p,$pnum)
            ->select();
        //更新用户所有数据为已读
        //将本页用户未读的消息更新为已读
        $save_id = [];
        foreach ($data as $val) {
            if($val['msg_display'] == 0){
                $save_id[] = $val['msg_id'];
            }
        }
        if(!empty($save_id)){
            $res = M('msg')
                ->where(['msg_id'=>['in',$save_id]])
                ->save(['msg_display'=>1]);
        }
        $this->backToClient(1,'查询成功',$data);
    }

    /**
     *获取用户分享成长中分享值列表数据
     *@author chen
     *@param user_id 用户ID(必选)
     *@param p 页码 默认1(可选)
     *@param pnum 每页条数 默认10(可选)
     *
     */
    public function getUserShareValueListData()
    {
        $json_arr = $this->Security();
//        $json_arr['user_id'] = 1000021;
//        $json_arr['p'] = 1;
//        $json_arr['pnum'] = 10;
        if(empty($json_arr['user_id'])){
            $this->backToClient(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $data = M('user_share')
            ->field('user_share_by_userid,user_share_value,user_share_createtime')
            ->where(['user_share_userid'=>$json_arr['user_id']])
            ->order('user_share_createtime desc')
            ->page($p,$pnum)
            ->select();
        if(!empty($data)){
            $arr_user = [];
            foreach ($data as $value) {
                $arr_user[] = $value['user_share_by_userid'];
            }
            //查询所有用户手机号
            $user_mobile_data = M('user')
                ->where($arr_user)
                ->getField('user_id,user_phone');
            foreach ($data as &$value) {
                foreach ($user_mobile_data as $k => $v) {
                    if($value['user_share_by_userid'] == $k){
                        $value['user_phone'] = substr_replace($v,'****',3,4);
                    }
                }
            }
            $this->backToClient(1,'获取成功',$data);
        }else{
            $this->backToClient(1,'获取成功');
        }
    }

    /**
     * 分享成长--收入明细列表数据
     * @author chen
     * @param user_id 用户ID(必选)
     * @param p 页码 默认1(可选)
     * @param pnum 每页条数 默认10(可选)
     */
    public function getUserShareMoneyListData()
    {
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1000021;
        // $json_arr['p'] = 1;
        // $json_arr['pnum'] = 10;
        if(empty($json_arr['user_id'])){
            $this->backToClient(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $data = M('user_money_transact')
            ->field('user_money_transact_describe,user_money_transact_num,user_money_transact_createtime')
            ->where([
                'user_money_transact_userid'=>$json_arr['user_id'],
                'user_money_transact_type'=>2,
            ])
            ->page($p,$pnum)
            ->order('user_money_transact_createtime desc')
            ->select();
        $this->backToClient(1,'查询成功',$data);
    }

    /**
     * 用户金币交易明细
     * @author chen
     * @param user_id 用户ID(必选)
     * @param p 页码 默认1(可选)
     * @param pnum 每页条数 默认10(可选)
     */
    public function getUserMoneyTransactListData()
    {
        $json_arr = $this->Security();
        // $json_arr['user_id'] = 1000021;
        // $json_arr['p'] = 1;
        // $json_arr['pnum'] = 10;
        if(empty($json_arr['user_id'])){
            $this->backToClient(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $data = M('user_money_transact')
            ->field('user_money_transact_describe,user_money_transact_num,user_money_transact_createtime,user_money_transact_type')
            ->where([
                'user_money_transact_userid'=>$json_arr['user_id'],
            ])
            ->page($p,$pnum)
            ->order('user_money_transact_createtime desc')
            ->select();
        $this->backToClient(1,'查询成功',$data);
    }

    /**
     * 获取关于我们 吃苹果规则h5路径
     * @author chen
     *
     */
    public function getAboutUsAppleH5()
    {
        $json_arr = $this->Security();
        $data = M('helpdoc')->select();
        $this->backToClient(1,'获取成功',$data);
    }

    /**
     * 检查版本
     * @author chen
     * @param version_type 客户端类型 1安卓 2苹果（必选）
     * @param  version_code 版本号(必选)
     *
     */
    public function checkVersion()
    {
        $json_arr = $this->Security();
//         $json_arr['version_type'] = 1;
        if(empty($json_arr['version_type'])){
            $this->backToClient(2,'参数不全');
        }
        $info = M('version')
            ->where(['version_type'=>$json_arr['version_type']])
            ->order('version_code desc')
            ->find();
        $this->backToClient(1,'获取成功',$info);
    }

    /**
     * 获取用户订单列表数据(可根据订单状态获取数据)
     * @author chen
     * @param order_userid 用户ID(必选)
     * @param order_status 订单状态 (可选)
     * 1待付款 2已取消 3已付款待发货 4已发货待收货 5已收货 6已删除
     *
     */
    public function getOrderListData()
    {
        $json_arr = $this->Security();
//        $json_arr['order_userid'] = 1000025;
//        $json_arr['order_status'] = 1;
        if(empty($json_arr['order_userid'])){
            $this->backToClient(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        if($json_arr['order_status'] > 0){
            $where['order_status'] = $json_arr['order_status'];
        }
        $where['order_userid'] = $json_arr['order_userid'];
        $data = M('order')
            ->field('order_id,order_userid,order_sn,order_goods_name,order_goods_img,order_goods_num,order_goods_kind,order_money,order_status')
            ->where($where)
            ->page($p,$pnum)
            ->order('order_createtime desc')
            ->select();
        if(!empty($data)){
            foreach ($data as &$val) {
                if(!empty($val['order_goods_img'])){
                    $val['order_goods_img'] = explode(',',$val['order_goods_img']);
                }
            }
        }
        $this->backToClient(1,'获取成功',$data);
    }

    /**
     * 修改订单状态
     * @author chen
     * @param order_id 订单ID(必选)
     * @param order_userid 用户ID(必选)
     * @param order_status 修改成的订单状态 2取消支付 5确认收货 6超时未付款的过期订单 7已删除(必选)
     *
     */
    public function updateOrderStatus()
    {
        $json_arr = $this->Security();
//        $json_arr['order_id'] = 1;
//        $json_arr['order_userid'] = 1000025;
//        $json_arr['order_status'] = 5;
        if(empty($json_arr['order_userid']) || empty($json_arr['order_status'])){
            $this->backToClient(2,'参数不全');
        }
        $order_model = M('order');
        $order_info = $order_model->field('order_status')->find($json_arr['order_id']);
        if(!empty($order_info)){
            //取消订单操作
            if($json_arr['order_status'] == 2 && $order_info['order_status'] == 1){
                $res = M('order')
                    ->where([
                        'order_id'=>$json_arr['order_id'],
                        'order_userid'=>$json_arr['order_userid'],
                    ])
                    ->save(['order_status'=>$json_arr['order_status']]);
            }else if($json_arr['order_status'] == 5 && $order_info['order_status'] == 4){
                $res = M('order')
                    ->where([
                        'order_id'=>$json_arr['order_id'],
                        'order_userid'=>$json_arr['order_userid'],
                    ])
                    ->save([
                        'order_status'=>$json_arr['order_status'],
                        'order_receivetime'=>time(),
                    ]);
            }else if($json_arr['order_status'] == 7 &&
                ($order_info['order_status'] == 1 ||
                    $order_info['order_status'] == 2 ||
                    $order_info['order_status'] == 5 ||
                    $order_info['order_status'] == 6
                )
            ){
                $res = M('order')
                    ->where([
                        'order_id'=>$json_arr['order_id'],
                        'order_userid'=>$json_arr['order_userid'],
                    ])
                    ->save(['order_status'=>$json_arr['order_status']]);
            }else{
                $res = false;
            }
        }else{
            $this->backToClient(3,'不存在该订单');
        }
        if($res){
            $this->backToClient(1,'操作成功');
        }else{
            $this->backToClient(4,'操作有误');
        }
    }

    /**
     * 获取订单详情
     * @author chen
     * @param order_id 订单ID(必选)
     * @param order_userid 用户ID(必选)
     *
     */
    public function getOrderDetail()
    {
        $json_arr = $this->Security();
//        $json_arr['order_id'] = 3;
//        $json_arr['order_userid'] = 1000025;
        if(empty($json_arr['order_id']) || empty($json_arr['order_userid'])){
            $this->backToClient(2,'参数不全');
        }
        $info = M('order')
            ->field('order_client_type,order_pay_type,order_updatetime,order_sendtime,order_receivetime',true)
            ->where(['order_id'=>$json_arr['order_id'],'order_userid'=>$json_arr['order_userid']])
            ->find();
        $info['order_receive_info'] = json_decode($info['order_receive_info'],true);
        if(!empty($info['order_details'])){
            //查询订单中商品信息
            $goods_details_arr = setGoodsDetailData($info['order_details']);
            $goods_select = M('goods')
                ->field('goods_id,goods_name,goods_describe,goods_price,goods_price_vip,goods_pic1,goods_display')
                ->where(['goods_id'=>['in',$goods_details_arr['goods_id_arr']]])
                ->select();
            foreach ($goods_details_arr['data'] as $key => $value) {
                foreach ($goods_select as $v) {
                    if($value['goods_id'] == $v['goods_id']){
                        $v['goods_num'] = $value['goods_num'];
                        $v['goods_buy_price'] = $value['goods_buy_price'];
                        $goods_details_arr['data'][$key] = $v;
                    }
                }
            }
            $info['goods_data'] = $goods_details_arr['data'];
            $this->backToClient(1,'获取成功',$info);
        }else{
            $this->backToClient(3,'订单信息有误');
        }
    }

    /**
     * 商品支付接口 支付宝 微信
     * @author chen
     * @param order_userid 用户ID(必选)
     * @param order_goods_kind 商品种类(必选)
     * @param order_goods_name 多个商品时只商品名称之间用逗号隔开(必选)
     * @param order_goods_img 订单中商品图片 多种商品时图片之间用逗号隔开(必选)
     * @param order_goods_num 订单商品总数量(必选)
     * @param order_money 订单总价(必选)
     * @param order_details 商品详情 (商品ID : 商品数量 : 商品单价) 多商品之间用逗号连接(必选)
     * @param order_client_type 客户端类型 1安卓 2 IOS(必选)
     * @param order_pay_type 支付类型 1支付宝 2微信 (必选)
     * @param receive_name 收货人姓名(必选)
     * @param receive_phone 收货人电话(必选)
     * @param receive_address 收货人地址(必选)
     */
    public function createOrder()
    {
        $json_arr = $this->Security();
//        $json_arr['order_userid'] = 1000025;
//        $json_arr['order_goods_kind'] = 3;
//        $json_arr['order_goods_name'] = '';
//        $json_arr['order_goods_img'] = '/Upload/image/201710/16/123213.jpg,/Upload/image/201710/16/123213.jpg';
//        $json_arr['order_goods_num'] = 8;
//        $json_arr['order_money'] = 99.99;
//        $json_arr['order_details'] = '8:8:99.99,9:5:109.99';
//        $json_arr['order_client_type'] = 1;
//        $json_arr['order_pay_type'] = 1;
//        $json_arr['receive_name'] = '小雷';
//        $json_arr['receive_phone'] = '13812345888';
//        $json_arr['receive_address'] = '北京朝阳区';
        $json_arr['order_sn'] = 'o'.createOrdernum();
        $json_arr['order_createtime'] = time();
        if(empty($json_arr['order_userid']) ||
            empty($json_arr['order_goods_kind']) ||
            empty($json_arr['order_goods_img']) ||
            empty($json_arr['order_goods_num']) ||
            empty($json_arr['order_money']) ||
            empty($json_arr['order_details']) ||
            empty($json_arr['order_client_type']) ||
            empty($json_arr['order_pay_type']) ||
            empty($json_arr['receive_name']) ||
            empty($json_arr['receive_phone']) ||
            empty($json_arr['receive_address'])
        ){
            $this->backToClient(2,'参数不全');
        }
        if($json_arr['order_goods_kind'] == 1 && empty($json_arr['order_goods_name'])){
            $this->backToClient(2,'参数不全');
        }
        $json_arr['order_receive_info'] = json_encode(
            [
                'receive_name'=>$json_arr['receive_name'],
                'receive_phone'=>$json_arr['receive_phone'],
                'receive_address'=>$json_arr['receive_address'],
            ],JSON_UNESCAPED_UNICODE);
        //生成订单
        $add_res = M('order')->add($json_arr);
        $json_arr['order_id'] = $add_res;
        if($add_res){
            $this->backToClient(1,'创建订单成功',$json_arr);
        }else{
            $this->backToClient(3,'创建订单失败');
        }
    }

    /**
     * ios 创建用户充值vip 用户充钱换金币的订单
     * @author chen
     * @param user_money_userid 用户ID(必选)
     * @param user_money_num 充值金额(必选)
     * @param user_money_type 1充值兑金币 2 VIP会员(必选)
     * @param user_money_valid_num 充值金币数或VIP会员天数(必选)
     *
     */
    public function setIosUserRechargeMoney()
    {
        $json_arr = $this->Security();
//        $json_arr['user_money_userid'] = 1000025;
//        $json_arr['user_money_num'] = 30;
//        $json_arr['user_money_type'] = 2;
//        $json_arr['user_money_valid_num'] = 30;
        if(empty($json_arr['user_money_userid']) ||
            empty($json_arr['user_money_num']) ||
            empty($json_arr['user_money_type']) ||
            empty($json_arr['user_money_valid_num'])
        ){
            $this->backToClient(2,'参数不全');
        }
        if($json_arr['user_money_type'] == 1){
            $json_arr['user_money_describe'] = '充钱兑换金币';
            $json_arr['user_money_sn'] = 'm'.createOrdernum();
        }else if($json_arr['user_money_type'] == 2){
            $json_arr['user_money_describe'] = '充值VIP会员';
            $json_arr['user_money_sn'] = 'v'.createOrdernum();
        }
        $json_arr['user_money_client'] = 2;
        $json_arr['user_money_pay_type'] = 3;
        $json_arr['user_money_createtime'] = time();
        $res = M('user_money')->add($json_arr);
        if($res){
            $return_data = [
                'user_money_id'=>$res,
                'user_money_sn'=>$json_arr['user_money_sn']
            ];
            $this->backToClient(1,'创建订单成功',$return_data);
        }else{
            $this->backToClient(3,'创建订单失败');
        }
    }

    /**
     * ios 处理用户充值vip 用户充钱换金币的订单
     * @author chen
     * @param user_money_id 充值记录表ID(必选)
     * @param user_money_userid 充值用户ID(必选)
     * @param user_money_sn 充值订单号(必选)
     *
     */
    public function doIosUserRechargeMoney()
    {
        $json_arr = $this->Security();
//        $json_arr['user_money_id'] = 10;
//        $json_arr['user_money_userid'] = 1000025;
//        $json_arr['user_money_sn'] = 'v1386231013241424';
        if(empty($json_arr['user_money_id']) || empty($json_arr['user_money_userid']) || empty($json_arr['user_money_sn'])){
            $this->backToClient(2,'参数不全');
        }
        //查询该充钱订单信息
        $money_model = M('user_money');
        $where['user_money_id'] = $json_arr['user_money_id'];
        $where['user_money_userid'] = $json_arr['user_money_userid'];
        $where['user_money_sn'] = $json_arr['user_money_sn'];
        $money_info = $money_model
            ->field('user_money_num,user_money_valid_num,user_money_type,user_money_status')
            ->where($where)
            ->find();
        if(!empty($money_info) && $money_info['user_money_status'] == 1){
            //更新充钱订单状态
            $res = doingUserMoneySuccess($where['user_money_id'],$where['user_money_userid'],$money_info['user_money_num'],$money_info['user_money_type'],$money_info['user_money_valid_num']);
            if($res){
                $this->backToClient(1,'支付成功');
            }else{
                $this->backToClient(4,'支付失败');
            }
        }else if(!empty($money_info) && $money_info['user_money_status'] == 3){
            $this->backToClient(1,'已经支付过,不能重复支付');
        }else{
            $this->backToClient(3,'没有该充钱记录');
        }
    }

    /**
     * 安卓 充钱换金币接口
     * @author chen
     * @param user_money_userid 用户ID(必选)
     * @param user_money_num 充值金额(必选)
     * @param user_money_valid_num 充值金币数或VIP会员天数(必选)
     * @param user_money_pay_type 支付类型(必选) 1支付宝 2微信 3 ApplePay
     *
     */
    public function createMoneyOrder()
    {
        $json_arr = $this->Security();
//        $json_arr['user_money_userid'] = 1000025;
//        $json_arr['user_money_num'] = 100;
//        $json_arr['user_money_valid_num'] = 150;
//        $json_arr['user_money_pay_type'] = 1;
        if(empty($json_arr['user_money_userid']) ||
            empty($json_arr['user_money_num']) ||
            empty($json_arr['user_money_valid_num']) ||
            empty($json_arr['user_money_pay_type'])
        ){
            $this->backToClient(2,'参数不全');
        }
        $json_arr['user_money_sn'] = 'm'.createOrdernum();
        $json_arr['user_money_describe'] = '充钱兑换金币';
        $json_arr['user_money_type'] = 1;//1充值兑金币 2 VIP会员
        $json_arr['user_money_client'] = 1;//1安卓 2 IOS
        $json_arr['user_money_createtime'] = time();
        $res = M('user_money')->add($json_arr);
        $json_arr['user_money_id'] = $res;
        $info = $json_arr;
        $this->backToClient(1,'生成兑换金币订单成功',$info);
    }

    /**
     * 安卓 用户充VIP接口
     * @author chen
     * @param user_money_userid 用户ID(必选)
     * @param user_money_num 充值金额(必选)
     * @param user_money_describe 充值描述信息(必选)
     * @param user_money_valid_num 充值金币数或VIP会员天数(必选)
     * @param user_money_pay_type 支付类型(必选) 1支付宝 2微信 3 ApplePay
     *
     */
    public function createVipOrder()
    {
//         $json_arr = $this->Security();
        $json_arr['user_money_userid'] = 1000025;
        $json_arr['user_money_num'] = 1500;
        $json_arr['user_money_valid_num'] = 360;
        $json_arr['user_money_pay_type'] = 1;
        if(empty($json_arr['user_money_userid']) ||
            empty($json_arr['user_money_num']) ||
            empty($json_arr['user_money_valid_num']) ||
            empty($json_arr['user_money_pay_type'])){
            $this->backToClient(2,'参数不全');
        }
        $json_arr['user_money_sn'] = 'v'.createOrdernum();
        $json_arr['user_money_describe'] = '充值VIP会员';
        $json_arr['user_money_type'] = 2;//1充值兑金币 2 VIP会员
        $json_arr['user_money_client'] = 1;//1安卓 2 IOS
        $json_arr['user_money_createtime'] = time();
        $res = M('user_money')->add($json_arr);
        $json_arr['user_money_id'] = $res;
        $info = $json_arr;
        $this->backToClient1(1,'生成VIP会员订单成功',$info);
    }

    //支付宝异步回调处理--商品支付
    public function doAliPayGoodsBack()
    {
//        $order_sn = '1386135633259613';
        $order_sn = I('order_sn');
        $order_model = M('order');
        $order_info = $order_model->where(['order_sn'=>$order_sn])->find();
        if(!empty($order_info)){
            if($order_info['order_status'] == 1){
                $res = doPaySuccessOrder($order_info);
                if($res){
                    echo 'success';
                }else{
                    echo 'error1';
                }
            }else{
                echo 'error2';
            }
        }else{
            echo 'error3';//未找到该订单数据
        }
    }

    //支付宝异步回调处理--充值兑换金币和用户充VIP回调方法
    public function doAliPayUserMoneyBack()
    {
        $json_arr['user_money_sn'] = 'y1386233329457641';
        if(empty($json_arr['user_money_sn'])){
            $this->backToClient1(2,'参数不全');
        }
        //查询该充钱订单信息
        $money_model = M('user_money');
        $where['user_money_sn'] = $json_arr['user_money_sn'];
        $money_info = $money_model
            ->field('user_money_id,user_money_userid,user_money_num,user_money_valid_num,user_money_type,user_money_status')
            ->where($where)
            ->find();
        if(!empty($money_info) && $money_info['user_money_status'] == 1){
            //更新充钱订单状态
            $res = doingUserMoneySuccess($money_info['user_money_id'],$money_info['user_money_userid'],$money_info['user_money_num'],$money_info['user_money_type'],$money_info['user_money_valid_num']);
            if($res){
                $this->backToClient1(1,'支付成功');
            }else{
                $this->backToClient1(4,'支付失败');
            }
        }else if(!empty($money_info) && $money_info['user_money_status'] == 3){
            $this->backToClient1(1,'已经支付过,不能重复支付');
        }else{
            $this->backToClient1(3,'没有该充钱记录');
        }
    }

    /**
     * 关于我们的h5页面
     *
     */
    public function getAboutUs()
    {
        $this->display();
    }

    /**
     * 生成验证码
     * @author chen
     */
    public function showVerify(){
        show_verify();
    }

    /**
     *H5 注册页面
     *
     */
    //注册
    public function H5register()
    {
        if(IS_POST){
            if (empty($_POST['user_phone']) || empty($_POST['user_password'])) {
                $this->redirect('error',array('status'=>2));
            }

            if (!isMobile($_POST['user_phone'])) {
                $this->redirect('error',array('status'=>3));
            }

            if(!isPassword($_POST['user_password'])){
                $this->redirect('error',array('status'=>7));
            }

            $data['user_phone'] = $_POST['user_phone'];
            $user_arr = M('user')->where($data)->find();
            if(!empty($user_arr)){
                if ($user_arr['user_display'] == 0) {
                    $this->redirect('error',array('status'=>4));
                }
                $this->redirect('error',array('status'=>5));
            }
            $condition['user_id_introducer'] = $_POST['user_id_introducer'];
            $condition['user_phone'] = $_POST['user_phone'];
            $condition['user_name'] = phonenumHidden($_POST['user_phone']);
            $condition['user_regtime'] = time();
            $condition['user_password'] = md5((md5($_POST['user_password'].C(AES_KEY))).C(AES_KEY));
            $user_model = M('user');
            M()->startTrans();
            $user_id = $user_model->data($condition)->add();
            $user_sig = $user_model->save([
                'user_id'=>$user_id,
                'user_sig'=>$this->getUserSig($user_id),
                'user_sigexpire'=>time() + 60*60*24*150, //150天过期时间
            ]);
            if($condition['user_id_introducer'] > 0){
                //介绍人苹果数增加
                $by_user_share = $user_model
                    ->where(['user_id'=>$condition['user_id_introducer']])
                    ->setInc('user_apple_val',50);//介绍人所得分享值数未定
                //添加记录
                $apple_val_log = M('user_share')
                    ->add([
                        'user_share_userid'=>$condition['user_id_introducer'],
                        'user_share_by_userid'=>$user_id,
                        'user_share_value'=>50,//介绍人所得分享值
                        'user_share_createtime'=>time(),
                    ]);
            }else{
                $by_user_share = true;
                $apple_val_log = true;
            }
            if($user_id && $user_sig && $by_user_share && $apple_val_log){
                M()->commit();
                $this->redirect('success');
            }else{
                $this->redirect('error',array('status'=>6));
            }
        }else{
            $user_id_introducer = I('user_id',1000022,'intval');
            if($user_id_introducer > 0){
                $find_res = M('user')->field('user_display')->find($user_id_introducer);
                if(!empty($find_res)){
                    if($find_res['user_display'] == 1){
                        $this->assign('user_id_introducer',$user_id_introducer);
                    }else{
                        $this->assign('user_id_introducer',0);
                    }
                }else{
                    $this->assign('user_id_introducer',0);
                }
            }else{
                $this->assign('user_id_introducer',0);
            }
            $this->display();
        }
    }

    /**
     * 发送短信验证码（WEB）
     * @param user_phone      要发送的手机号码
     */
    public function sendMsgCodeWeb(){

        // $_POST['user_phone'] = '17301181236';

        if (!check_verify($_POST['verify'])) {
            echo json_encode(array('status'=>2,'msg'=>'图形验证码错误'));
            exit();
        }

        $phonenums = $_POST['user_phone'];
        $code = createRandomCode(4);
        alisms($code, $phonenums); //发送短信
        echo json_encode(array('status'=>1,'msg'=>'获取短信验证码成功','data'=>$code));
        exit();
    }

    /**
     * 注册失败
     * @param status      错误码
     */
    public function error(){
        $status = $_GET['status'];
        $msg = '';
        switch ($status) {
            case '2':
                $msg = '参数不全';
                break;
            case '3':
                $msg = '手机号码格式错误';
                break;
            case '4':
                $msg = '该账号存在非法操作，详情请咨询客服';
                break;
            case '5':
                $msg = '号码已被注册';
                break;
            case '6':
                $msg = '注册失败';
                break;
            case '7':
                $msg = '密码格式不符';
                break;
            default:
                $msg = '注册失败';
                break;
        }
        $this->assign('msg',$msg);
        $this->display();
    }
    /**
     * 注册成功
     */
    public function success(){
        //ios最新下载地址
        $ios_arr = M('version')->where(array('version_type'=>2))->order('version_code desc')->find();
        $this->assign('ios_url',$ios_arr['version_url']);
        //android最新下载地址
        $android_arr = M('version')->where(array('version_type'=>1))->order('version_code desc')->find();
        $this->assign('android_url',$android_arr['version_url']);
        $this->display();
    }

}
